#include "System_Init.h"
#include "fsl_port.h"
#include "fsl_pit.h"

static void init_leds(void)
{
    /* Define the init structure for the output LED pin*/
    gpio_pin_config_t led_config = {
        kGPIO_DigitalOutput, 1,
    };
    
    GPIO_PinInit(BOARD_INITPINS_LED_RED_GPIO, BOARD_INITPINS_LED_RED_PIN, &led_config);
    GPIO_PinInit(BOARD_INITPINS_LED_BLUE_GPIO, BOARD_INITPINS_LED_BLUE_PIN, &led_config);
    GPIO_PinInit(BOARD_INITPINS_LED_GREEN_GPIO, BOARD_INITPINS_LED_GREEN_PIN, &led_config);
    
    return;
}

static void init_pit(void)
{
    /* Structure of initialize PIT */
    pit_config_t pitConfig;
    
/*
     * pitConfig.enableRunInDebug = false;
     */
    PIT_GetDefaultConfig(&pitConfig);

    /* Init pit module */
    PIT_Init(PIT, &pitConfig);

    /* Set timer period for channel 0 */
    PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(10000U, CLOCK_GetFreq(kCLOCK_BusClk)));

    /* Enable timer interrupts for channel 0 */
    PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable);

    /* Enable at the NVIC */
    EnableIRQ(PIT0_IRQn); 

    /* Start channel 0 */
    PIT_StartTimer(PIT, kPIT_Chnl_0);
}
        
#if 0        
static void init_keys(void)
{
    /* Define the init structure for the output LED pin*/
    gpio_pin_config_t key_config = {
        kGPIO_DigitalInput, 0,
    };
    
    PORT_SetPinInterruptConfig(BOARD_INITPINS_BUTTON1_PORT, BOARD_INITPINS_BUTTON1_PIN, kPORT_InterruptFallingEdge);
    EnableIRQ(BUTTON1_IRQ);
    GPIO_PinInit(BOARD_INITPINS_BUTTON1_GPIO, BOARD_INITPINS_BUTTON1_PIN, &key_config);
    
    PORT_SetPinInterruptConfig(BOARD_INITPINS_BUTTON2_PORT, BOARD_INITPINS_BUTTON2_PIN, kPORT_InterruptFallingEdge);
    EnableIRQ(BUTTON2_IRQ);    
    GPIO_PinInit(BOARD_INITPINS_BUTTON2_GPIO, BOARD_INITPINS_BUTTON2_PIN, &key_config);
    
    return;
}
#endif
void init_system(void)
{
    init_leds();
    //init_keys();
    init_pit();
    return;
}